package com.eryos.gwt.estate.client.activity;

import com.allen_sauer.gwt.log.client.Log;
import com.eryos.gwt.estate.client.factory.ClientFactory;
import com.eryos.gwt.estate.client.place.ErrorPlace;
import com.eryos.gwt.estate.client.view.ErrorView;
import com.google.gwt.place.shared.Place;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.web.bindery.event.shared.EventBus;

/**
 * An activity simply represents something the user is doing. An Activity contains no Widgets or UI code. Activities
 * typically restore state ("wake up"), perform initialization ("set up"), and load a corresponding UI ("show up").
 * 
 * Activities are started and stopped by an ActivityManager associated with a container Widget. An Activity can
 * automatically display a warning confirmation when the Activity is about to be stopped (such as when the user
 * navigates to a new Place).
 * 
 * In addition, the ActivityManager warns the user before the window is about to be closed.
 * 
 * @author combeol
 * @version $Revision$
 */
public class ErrorActivity extends BaseActivity implements ErrorView.Presenter {

    // Used to obtain views, eventBus, placeController
    // Alternatively, could be injected via GIN
    private final ClientFactory clientFactory;
    private ErrorView errorView;

    /**
     * 
     * @param place
     * @param clientFactory
     */
    public ErrorActivity(final ErrorPlace place, final ClientFactory clientFactory) {
        /************ Init Params ************/

        /************ Retrieve Params ************/
        exception = place.getException();

        this.clientFactory = clientFactory;
    }

    Exception exception;

    /**
     * The start method is invoked by the ActivityManager and sets things in motion. It updates the view and then
     * swaps the view back into the Activity's container widget by calling setWidget.
     */
    @Override
    public void start(final AcceptsOneWidget containerWidget, final EventBus eventBus) {
        Log.info("start ErrorActivity");
        errorView = clientFactory.getErrorView();

        /************ Pass Params ************/

        errorView.setPresenter(this);
        errorView.setException(exception);

        retrieveDataFromServer();

        containerWidget.setWidget(errorView.asWidget());
        // Stop waiting message ?
    }

    /**
     * The non-null mayStop() method provides a warning that will be shown to the user when the Activity is about to
     * be stopped due to window closing or navigation to another Place.
     */
    @Override
    public String mayStop() {

        // Start waiting message ?
        return null;

        // Uncomment the following line to raise a warning to the user
        // return "Please hold on. This activity is stopping.";
    }

    /**
     * Method used to retrieve data from Server/Service before populating View.
     */
    private void retrieveDataFromServer() {
        // clientFactory.getService().getXXX(new AsyncCallback<XX>(){
        //
        // @Override
        // public void onFailure(Throwable caught){
        // logger.log(Level.ERROR, "getXXX : Failure", caught);
        // }
        //
        // @Override
        // public void onSuccess(XX result){
        // logger.log(Level.DEBUG, "getXXX : Success");
        // // TODO
        //
        // refreshView();
        // }
        // });
    }

    /**
     * Called after data retrieved from Server (async call) to update the view.
     */
    private void refreshView() {
        /************ View Setters ************/

    }

    /**
     * Method called after a View action, to retrieve values to send to Server.
     */
    private void retrieveDataFromView() {
        /************ View Getters ************/

    }

    /**
     * Called after retrieveDataFromView to update the Server/Service/DB with new values.
     */
    private void refreshDataOnServer() {
        // retrieveDataFromView();
        //
        // clientFactory.getService.updateXXX(XXX, new AsyncCallback<Boolean>(){
        //
        // @Override
        // public void onSuccess(Boolean result){
        // logger.log(Level.DEBUG, "updateXX : Success");
        // // TODO
        //
        // }
        // });
    }

    /**
     * The goTo() method invokes the PlaceController to navigate to a new Place.
     */
    @Override
    public void goTo(Place place) {
        clientFactory.getPlaceController().goTo(place);
    }

}
